package net.onceface.application.assembler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.onceface.application.dto.OrderDTO;
import net.onceface.core.domain.Order;

public class OrderAssembler {

	/**
	 * 转化DTO 
	 * @param order
	 * @param true-转化映射关系 false-只转化本类
	 * **/
	public static OrderDTO toDTO(Order order, boolean b){
		if(order == null){
			return null;
		}
		OrderDTO result = new OrderDTO();
		result.setId(order.getId());
		result.setVersion(order.getVersion());
		result.setOrderNumber(order.getOrderNumber());
		result.setTotalPrice(order.getTotalPrice());
		result.setPrice(order.getPrice());
		result.setDiscount(order.getDiscount());
		result.setClientId(order.getClientId());
		result.setCreateDate(order.getCreateDate());
		result.setPayment(order.getPayment());
		result.setPaymentName(order.getPayment().getLabel());
		result.setOrderStatus(order.getOrderStatus());
		result.setOrderStatusName(order.getOrderStatus().getLabel());
		result.setReceiver(order.getReceiver());
		result.setPhone(order.getPhone());
		result.setAddress(order.getAddress());
		if(order.getDisCountId() != null && order.getDisCountId() > 0){
			result.setDisCountId(order.getDisCountId());
		}
		if(order.getCouponsId() != null){
			result.setCouponsId(order.getCouponsId());
		}
		if(order.getExpressNumber() != null){
			result.setExpressNumber(order.getExpressNumber());
		}
		if(order.getExpressName() != null){
			result.setExpressName(order.getExpressName());
		}
		if(order.getRemarks() != null){
			result.setRemarks(order.getRemarks());
		}
		if(b){
			result.setOrderProducts(OrderProductAssembler.toDTOs(order.getOrderProducts()));
		}
		
		return result;
	}
	/**
	 * 转化DTO 
	 * @param order
	 * @param true-转化映射关系 false-只转化本类
	 * **/
	public static Order toEntity(OrderDTO order){
		if(order == null){
			return null;
		}
		Order result = new Order();
		result.setId(order.getId());
		result.setVersion(order.getVersion());
		result.setOrderNumber(order.getOrderNumber());
		result.setTotalPrice(order.getTotalPrice());
		result.setPrice(order.getPrice());
		result.setDiscount(order.getDiscount());
		result.setClientId(order.getClientId());
		result.setCreateDate(order.getCreateDate());
		result.setPayment(order.getPayment());
		result.setOrderStatus(order.getOrderStatus());
		result.setReceiver(order.getReceiver());
		result.setPhone(order.getPhone());
		result.setAddress(order.getAddress());
		if(order.getDisCountId() != null && order.getDisCountId() > 0){
			result.setDisCountId(order.getDisCountId());
		}
		if(order.getCouponsId() != null){
			result.setCouponsId(order.getCouponsId());
		}
		if(order.getExpressNumber() != null){
			result.setExpressNumber(order.getExpressNumber());
		}
		if(order.getExpressName() != null){
			result.setExpressName(order.getExpressName());
		}
		if(order.getRemarks() != null){
			result.setRemarks(order.getRemarks());
		}
		if(order.getOrderProducts() != null){
			result.setOrderProducts(OrderProductAssembler.toEntities(order.getOrderProducts()));
		}
		return result;
	}
	
	public static List<Order>  toEntities(Collection<OrderDTO>  orderDTOs){
		if (orderDTOs == null) {
			return null;
		}
		List<Order> results = new ArrayList<Order>();
		for (OrderDTO each : orderDTOs) {
			results.add(toEntity(each));
		}
		return results;
	}
	
	/**
	 * 转化DTO 
	 * @param order
	 * @param true-转化映射关系 false-只转化本类
	 * **/
	public static List<OrderDTO>  toDTOs(Collection<Order>  orders, boolean b){
		if (orders == null) {
			return null;
		}
		List<OrderDTO> results = new ArrayList<OrderDTO>();
		for (Order each : orders) {
			results.add(toDTO(each, b));
		}
		return results;
	}
}
